package com.bonc.ioc.demo.contorller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonc.ioc.demo.dao.UserMapper;
import com.bonc.ioc.demo.entity.User;
import com.bonc.ioc.demo.exception.McpException;
import com.bonc.ioc.demo.model.ValidationDemo;
import com.bonc.ioc.demo.service.UserService;
import com.bonc.ioc.demo.utils.ValidateUtils;
import com.bonc.ioc.demo.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @RequestMapping("/demo/updateName")
    @Transactional(rollbackFor = Exception.class)
    public String updateName(@RequestParam(name = "userName") String userName) throws Exception {
        log.info("updateName ...");
        User user = new User();
        user.setId(2);
        user.setUserName(userName);
        userMapper.updateById(user);
        User user1 = userMapper.selectById(2);
        String message = JSON.toJSONString(user1);
        return message;
    }

    @RequestMapping("/demo/session")
    public String testsqlSession() {
        // 比较原生的创建session方式
        SqlSession sqlSession = new SqlSessionTemplate(sqlSessionFactory);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        String str = JSON.toJSONString(userMapper.selectById(1));
        return str;
    }

    /**
     * mybatis-plus 分页查询
     *
     * @param current
     * @param size
     */
    @RequestMapping("/queryList")
    public UserVo queryList(@RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        UserVo userVo = new UserVo();
        IPage<User> page = new Page<>(current, size);
        userMapper.selectPage(page, null);
        userVo.setCurrent(current);
        userVo.setSize(size);
        userVo.setTotal(page.getTotal());
        userVo.setUserList(page.getRecords());
        return userVo;
    }

    /**
     * 校验框架的使用
     */
    public void validation() {
        ValidationDemo validationDemo = new ValidationDemo();
        String ret = ValidateUtils.validate(validationDemo);
        if (StringUtils.isEmpty(ret)) {
            throw new McpException(ret);
        }
    }

    @RequestMapping("/findAll")
    public List<User> findAll() {
        List<User> userList = userMapper.findAll();
        return userList;
    }

    @RequestMapping("/deleteById")
    public void delete(int id) {
        userMapper.deleteById(id);
    }

}
